package com.epam.kulibaba.components.catalog;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.testng.Reporter;

import com.epam.kulibaba.components.Component;
import com.epam.kulibaba.components.catalog.navigator.CatalogNavigatorPager;
import com.epam.kulibaba.helpers.Class.Product;
import com.google.common.collect.Lists;

public class ProductsCatalog extends Component {
	public static final String CATALOG = ".catalog";
	
	public static final String COMPARELINK = "div.compareLine a";

	@FindBy(css = ProductsCatalog.CATALOG)
	private WebElement catalog;

	
	@FindBy(css = ProductsCatalog.COMPARELINK)
	private WebElement compareLink;

	public WebElement getCompareLink() {
		return compareLink;
	}
	
	public WebElement getCatalog() {
		return catalog;
	}

	public static ProductsCatalog getProductsCatalog(WebDriver driver) {
		ProductsCatalog productsCatalog = PageFactory.initElements(driver,
				ProductsCatalog.class);
		return productsCatalog;
	}
	
	public static void goToCompare(ProductsCatalog productsCatalog) {
		Reporter.log("Go to Compare");
		productsCatalog.getCompareLink().click();
	}
	
	public static List<Product> getProducts(ProductsCatalog productCatalog) {
		List<Product> products = Lists.newArrayList();
		List<WebElement> itemProducts = productCatalog.getCatalog()
				.findElements(By.cssSelector(".item"));
		for (WebElement item : itemProducts) {
			products.add(ConvertItemToProduct(item));
		}
		return products;
	}

	private static Product ConvertItemToProduct(WebElement item) {
		Product product = new Product();
		product.name = item.findElement(By.cssSelector(".name a")).getText();
		String price = item.findElement(By.cssSelector(".price strong"))
				.getText();
		price = price.replaceFirst("грн", "").replaceFirst(" ", "");
		product.price = Double.parseDouble(price);
		return product;
	}

	public static void goToProductPage(ProductsCatalog productCatalog,
			int productNumber) {
		Reporter.log("In products catalog go to product number "
				+ productNumber + " page");
		List<WebElement> product = productCatalog.getCatalog().findElements(
				By.xpath(".//div[@class='name']/a"));
		product.get(productNumber - 1).click();		
	}

	public static String getLinkOnProductPage(ProductsCatalog productCatalog,
			int itemNumber) {
		Reporter.log("In products catalog get link on product " + itemNumber
				+ " page");
		List<WebElement> product = productCatalog.getCatalog().findElements(
				By.xpath(".//div[@class='name']/a"));
		String productHref = product.get(itemNumber - 1).getAttribute("href");
		System.out.println(productHref);
		return productHref;
	}
	
	public static String getNameToSearchProduct(ProductsCatalog productCatalog,
			int itemNumber) {
		Reporter.log("In products catalog get link on product " + itemNumber
				+ " page");
		List<WebElement> product = productCatalog.getCatalog().findElements(
				By.xpath(".//div[@class='name']/a"));
		String productNameToSearch = product.get(itemNumber - 1).getText();
		System.out.println(productNameToSearch);
		return productNameToSearch;
	}
	
	public static void addProductToCompare(ProductsCatalog productsCatalog,
			int productNumber) {
		Reporter.log("In products catalog add product number " + productNumber
				+ " to Compare");
		productsCatalog.getCatalog()
				.findElement(By.xpath("//div[@class='item']['productNumber']"))
				.findElement(By.xpath("//span[@rel='0']")).click();
	}

	public static void removeProductFromCompare(
			ProductsCatalog productsCatalog, int productNumber) {
		Reporter.log("Remove product" + productNumber + " from Compare");
		productsCatalog
				.getCatalog()
				.findElement(
						By.xpath("(//div[@class='item'])['productNumber']"))
				.findElement(By.xpath("//span[@rel='1']")).click();
	}

	public static void goToProductsCompare(ProductsCatalog productsCatalog,
			int productNumber) {
		Reporter.log("Go to products " + productNumber + " compare");
		productsCatalog.getCompareLink().click();
	}
	
	public static List<String> getProductsShortDescription(
			ProductsCatalog productsCatalog) {
		Reporter.log("Get products short description");
		List<String> productsShortDescription = new ArrayList<String>();
		List<WebElement> itemProducts = productsCatalog.getCatalog()
				.findElements(By.cssSelector(".item"));
		for (WebElement item : itemProducts) {
			productsShortDescription.add(item
					.findElement(By.cssSelector(".description")).getText()
					.toLowerCase());
		}
		System.out.println(productsShortDescription);
		return productsShortDescription;
	}
	
	public static List<String> getAllProductsListShortDescription(
			WebDriver driver, CatalogNavigatorPager catalogPager,
			CatalogNavigatorPager linkForward, ProductsCatalog productsCatalog) {
		String lp = catalogPager.getLastPage().getText();
		List<String> productsShortDescription = new ArrayList<String>();
		for (int index = 0; index < Integer.parseInt(lp); index++) {
			if (index > 0)
				CatalogNavigatorPager.goToForwardPage(linkForward);
			List<WebElement> itemProducts = productsCatalog.getCatalog()
					.findElements(By.cssSelector(".item"));
			for (WebElement item : itemProducts) {
				productsShortDescription.add(item
						.findElement(By.cssSelector(".description")).getText()
						.toLowerCase());
			}
			System.out.println(productsShortDescription);
		}
		return productsShortDescription;

	}

}
